package com.example.book.enums;

import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/7/17
 * Time: 9:42
 */
@Getter
public enum BookStatusEnum {
    DELETED(0, "删除"),
    NORMAL(1, "可借阅"),
    FORBIDDEN(2, "不可借阅");

    private Integer code;
    private String desc;

    BookStatusEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public static BookStatusEnum getStatusByCode(Integer code) {

        // 1. 传统方式
//        if (code == 0) {
//            return BookStatusEnum.DELETED;
//        } else if (code == 1) {
//            return BookStatusEnum.NORMAL;
//        } else {
//            return BookStatusEnum.FORBIDDEN;
//        }

        // 2. 第二种 switch case
//        switch(code) {
//            case 0: return BookStatusEnum.DELETED;
//            case 1: return BookStatusEnum.NORMAL;
//            case 2: return BookStatusEnum.FORBIDDEN;
//        }

        // 3. 第三种 lambda 表达式方式
        return Arrays.stream(BookStatusEnum.values()).filter(x -> x.getCode() == code).findFirst().get();

    }
}
